<?php

/* ----------------------------------------------
  Caloris: Administration Contact Editor

  $Id$
  $Author$

  Editor kontaktů
  ---------------------------------------------- */

require_once("../../../index.php");
Caloris\ACL::authenticate();

define('_MODULE_ACTIVE', 'contactmanager');

$xmlContactsF = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/contacts.xml');

if ($_REQUEST["function_name"] == 'contactInsert') {
    $xme = $xmlContactsF->xpath('//contact[last()]');
    //echo print_r($xme);
    $id = ($xme[0]["id"] + 1);

    $doc = new DOMDOcument;
    $doc->load(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/contacts.xml');
    $xpath = new DOMXpath($doc);

    foreach ($xpath->query('/core') as $node) {
        $addElement = $doc->createElement('contact');
        $node->appendChild($addElement);

        $idAttribute = $doc->createAttribute("id");
        $addElement->appendChild($idAttribute);

        $idAttributeAdmin = $doc->createAttribute("connect");
        $addElement->appendChild($idAttributeAdmin);

        $p1 = $doc->createTextNode($id);
        $idAttribute->appendChild($p1);

        $p2 = $doc->createTextNode($_POST["idfu"]);
        $idAttributeAdmin->appendChild($p2);

        $p3 = $doc->createElement("name", '');
        $addElement->appendChild($p3);
    }

    $doc->save(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/contacts.xml');
    if (class_exists('tidy')) {
        $tidy_options = array(
            'input-xml' => true,
            'output-xml' => true,
            'indent' => true,
            'wrap' => false,
            'indent-cdata' => true,
        );

        $tidy = new tidy();
        $tidy->parseFile(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/contacts.xml', $tidy_options, 'utf8');
        $tidy->cleanRepair();

        $xmm = simplexml_load_string($tidy);
        $xmm->asXML(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/contacts.xml');
    }

    header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/" . _MODULE_ACTIVE . "/contacts.detail.php?idf=" . $id);
    exit();
} elseif ($_REQUEST["function_name"] == "contacts_delete") {
    $idf = Caloris\Text::protect($_POST["idf"]);

    $doc = new DOMDOcument;
    $doc->load(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/contacts.xml');

    $xpath = new DOMXpath($doc);
    $qry = '//contact[@id="' . $idf . '"]';

    foreach ($xpath->query($qry) as $node) {
        $node->parentNode->removeChild($node);
    }

    $doc->save(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/contacts.xml');

    if (file_exists(_CALSET_PATHS_BASE . '/cal-bin/contacts/contacts_' . $idf . '.jpg')) {
        unlink(_CALSET_PATHS_BASE . '/cal-bin/contacts/contacts_' . $idf . '.jpg');
    }

    if ($_POST["j"] != 1) {
        header("location: " . $_SERVER["HTTP_REFERER"] . "");
        exit();
    } else {
        echo "{\"msg\": \"done\"}";
        exit();
    }
}

$content = '
<h1>Kontakty</h1>

<form action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/' . _MODULE_ACTIVE . '/index.php" id="rpp" method="post">
<p style="margin-bottom: 10px;"><input type="hidden" name="function_name" value="contactInsert"  />
<input type="submit"  value="Přidat kontakt" class="btn btn-primary" /></p>
</form>

<p style="padding: 10px 0;">Seznam vašich kontaktů může být zobrazen na vaší kontaktní stránce nebo jen jako úložné místo pouze pro vaši potřebu.</p>' . PHP_EOL;

$xmlContacts = $xmlContactsF->xpath('//contact');

// sort by name
function sort_trees($t1, $t2) {
    return strcoll($t1->name, $t2->name);
}

usort($xmlContacts, 'sort_trees');

if (count($xmlContacts) > 0) {
    $content .= '
<table class="table table-striped">
    <tr>
        <th style="width: 20px;">&nbsp;</th>
        <th style="width: 250px;">Jméno</th>
        <th style="width: 40px;">Zobrazit</th>
        <th>Adresa</th>
        <th>PSČ</th>
        <th>Město</th>
        <th class="hidden-phone">Kraj</th>
        <th class="hidden-phone">Země</th>
    </tr>' . PHP_EOL;

    for ($a = 0; $a < count($xmlContacts); $a++) {
        if ($xmlContacts[$a]->show == 1) {
            $showContacts = 'ano';
        } else {
            $showContacts = 'ne';
        }

        if ($xmlContacts[$a]->name == '') {
            $name = '<em>nevyplněno</em>';
        } else {
            $name = $xmlContacts[$a]->surname . ', ' . $xmlContacts[$a]->name;
        }

        $content .= '
    <tr id="trbl_' . $a . '" >
        <td style="width: 20px;">
<form id="formshw_' . $a . '" action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/contactmanager/index.php" name="trbl_' . $a . '" method="post" onsubmit="return false;">
<input type="hidden" name="function_name" value="contacts_delete"  />
<input type="hidden" name="idf" value="' . $xmlContacts[$a]["id"] . '"  />
<input type="submit" value="Smazat" id="formx_' . $a . '" class="btn btn-danger" />
</form>
        </td>
        <td><a href="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/contactmanager/contacts.detail.php?idf=' . $xmlContacts[$a]["id"] . '">' . $name . '</a></td>
        <td>' . $showContacts . '</td>
        <td>' . $xmlContacts[$a]->address . '</td>
        <td>' . $xmlContacts[$a]->zip . '</td>
        <td>' . $xmlContacts[$a]->city . '</td>
        <td class="hidden-phone">' . $xmlContacts[$a]->district . '</td>
        <td class="hidden-phone">' . $xmlContacts[$a]->country . '</td>
    </tr>' . PHP_EOL;
    }


    $content .= '</table>' . PHP_EOL;
} else {
    $content .= '<p>Žádné kontakty nenalezeny</p>';
}

$data["title"] = 'Kontakty';

$tmpl = new Caloris\Template;
$tmpl->input(array($content));
$tmpl->renderHead($data);
echo $tmpl->renderBody('caloris-admin');